/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PreferencesDBBackupRevert.java
 *
 * Created on Apr 22, 2012, 5:14:53 PM
 */
package alwv.preferences;

import alwv.ALWVApp;
import alwv.ALWVView;
import alwv.MainView;
import alwv.backups.Backup;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Application;
import org.joda.time.LocalDate;

/**
 *
 * @author Sam
 */
public class PreferencesDBBackupRevertPane extends javax.swing.JPanel {
    private MainView mainview;
    /** Creates new form PreferencesDBBackupRevert */
    public PreferencesDBBackupRevertPane(MainView mv) {
        mainview = mv;
        initComponents();
        populateComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(alwv.ALWVApp.class).getContext().getResourceMap(PreferencesDBBackupRevertPane.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jButton1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(6, 6, 6)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(173, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    int response = JOptionPane.showConfirmDialog(this, "Are you sure you want to revert the database? All changes made since this\n"
            + "backup will be lost. (You can recover them by reverting to the current backup,\n which will be generated now.)");
    if (response == 0) {
        doRevertDatabase();
    }// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

    private void doRevertDatabase() {
        String dblocation = ALWVApp.getApplication().getDbLocation();
        String backuplocation = ALWVApp.getApplication().getBackupLocation();
        
        ALWVApp.getApplication().disconnect();
            String backup = jComboBox1.getSelectedItem().toString();
        try {
            
            LocalDate today = new LocalDate(LocalDate.now());
            
            try {
                Backup cd = new Backup(dblocation + "current/LWV/",backuplocation + today + "/");//TODO: Load directories from preferences
            } catch (IOException ex) {
                Logger.getLogger(ALWVView.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            Backup.delete(new File(dblocation + "current/LWV"));
        } catch (IOException ex) {
            Logger.getLogger(PreferencesDBBackupRevertPane.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Backup cd = new Backup(backuplocation + backup,dblocation + "current/LWV/");
        } catch (IOException ex) {
            Logger.getLogger(PreferencesDBBackupRevertPane.class.getName()).log(Level.SEVERE, null, ex);
        }
        ALWVApp.startConnection();
        mainview.updateAllDatabaseObjects();
        
        JOptionPane.showMessageDialog(this, "Database successfully recovered. If you need to undo this, please revert to the new backup.", "Success", JOptionPane.INFORMATION_MESSAGE);
        
        
    }
    
    private void populateComboBox() {
        File folder = new File("C:/ALWV/db/backups/");
        File[] listOfFiles = folder.listFiles();
        String[] files = new String[listOfFiles.length];
        for (int i = 0; i < listOfFiles.length; i++) {
            files[i] = listOfFiles[i].getName();
        }
        
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(files));
    }
}
